package com.music.mapper;

import com.music.vo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 用户数据访问接口，定义数据库操作
 */
@Mapper
public interface UserMapper {
    /**
     * 插入新用户
     * @param user 用户对象（包含用户名、密码等字段）
     */
    @Insert("INSERT INTO users " +
            "(Username, Password, Avatar, PhoneNumber, Status) " +
            "VALUES (#{username}, #{password}, #{avatar}, #{phoneNumber}, #{status})")
    void insertUser(User user);

    /**
     * 根据用户名查询用户
     * @param username 用户名
     * @return 用户对象（包含数据库查询结果）
     */
    @Select("SELECT UserID AS userId, Username AS username, Password AS password, " +
            "Avatar AS avatar, Time AS time, PhoneNumber AS phoneNumber, Status AS status " +
            "FROM users WHERE Username = #{username}")
    User selectByUsername(String username);

    /**
     * 根据手机号查询用户
     * @param phoneNumber 手机号
     * @return 用户对象（包含数据库查询结果）
     */
    @Select("SELECT UserID AS userId, Username AS username, Password AS password, " +
            "Avatar AS avatar, Time AS time, PhoneNumber AS phoneNumber, Status AS status " +
            "FROM users WHERE PhoneNumber = #{phoneNumber}")
    User selectByPhoneNumber(String phoneNumber);

    /**
     * 更新用户信息
     * @param user 包含用户ID和更新字段的用户对象
     */
    @Update("UPDATE users SET " +
            "Username = #{username}, " +
            "Password = #{password}, " +
            "Avatar = #{avatar}, " +
            "PhoneNumber = #{phoneNumber}, " +
            "Status = #{status} " +
            "WHERE UserID = #{userId}")
    void updateUser(User user);

    /**
     * 根据用户 ID 查询用户
     * @param userId 用户ID
     * @return 用户对象（包含数据库查询结果）
     */
    @Select("SELECT UserID AS userId, Username AS username, Password AS password, " +
            "Avatar AS avatar, Time AS time, PhoneNumber AS phoneNumber, Status AS status " +
            "FROM users WHERE UserID = #{userId}")
    User selectById(Integer userId);

    /**
     * 获取所有用户列表
     * @return 用户列表
     */
    @Select("SELECT UserID AS userId, Username AS username, Password AS password, " +
            "Avatar AS avatar, Time AS time, PhoneNumber AS phoneNumber, Status AS status " +
            "FROM users")
    List<User> selectAllUsers();
}